# -*- coding:utf-8 -*-
import subprocess
cmd = r"ping 8.8.8.8"
result = subprocess.Popen(cmd, stdout=subprocess.PIPE,shell=True)  # 将输出内容存至缓存中
while True:  # 将内容持续输出
     with open("ping.txt", "a", encoding="utf-8") as f:
         f.write(result.stdout.readline().decode("gbk").strip() + "\n")  # 先去除每一行末尾的制表符和换行符，然后再加上换行符，使写入文件中的内容不会有空行